<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class CategoryTable extends AbstractTable
{

    protected $table = 'hy_category';

    protected $id = 'category_id';

    public function find($query = array())
    {
        $select = new Select(array(
            'o' => $this->getTable()
        ));
//         $select->columns(array(
//             'category_id',
//             'category_name'
//         ));
        if (!empty($query['category_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('o.category_name', '%'.$query['category_name'] . "%");
            };
            
            $select->where($spec);
        }
        $select->order('o.category_id DESC');
        
        return $this->fetchPaginate($select, $query);
        
       
    }
    
    /**
     * 查询所有的分类 （包括显示和 不显示）
     */
    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }
    
    /**
     * 查询所有 显示的 商品分类
     */
    public function findAllDisplay($adminOrgId = '')
    {
        
        $where = array(
            'category_is_display' => 1
        );
        
        if($adminOrgId){
            $where['org_id'] = $adminOrgId;
        }
        
        $rowset = $this->select();
        return $rowset->toArray($where);
    }
    
    public function create($data)
    {
        return $this->insert($data);
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            $this->getId() => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function updateById($data, $id)
    {
        return $this->update($data, array(
            $this->getId() => $id
        ));
    }

    public function remove($id)
    {
        return $this->delete(array(
            $this->getId() => $id
        ));
    }
}